package evolution.interceptor;

import java.util.List;

import evolution.algorithm.CoevolutionAlgorithm;
import evolution.executor.EvolutionaryAlghorithmEpochRunner;
import evolution.individual.Individual;
import evolution.statistics.PopulationStatistics;
import evolution.statistics.PopulationStatisticsRegistry;

public class SimpleIterationInterceptor implements
		CoevolutionIterationInterceptor {

	private PopulationStatisticsRegistry registry = new PopulationStatisticsRegistry();

	@Override
	public void intercept(CoevolutionAlgorithm algorithm) {
		registerData(algorithm);
	}

	@Override
	public void finish(CoevolutionAlgorithm algorithm) {
		registerData(algorithm);
	}

	protected void registerData(CoevolutionAlgorithm algorithm) {
		List<EvolutionaryAlghorithmEpochRunner> evolutionaryAlghorithmEpochRunners = algorithm
				.getEvolutionaryAlghorithmEpochRunners();

		int size = evolutionaryAlghorithmEpochRunners.size();

		PopulationStatistics[] iterationData = new PopulationStatistics[size];
		for (int i = 0; i < size; i++) {
			Individual[] population = evolutionaryAlghorithmEpochRunners.get(i)
					.getEvolutionaryAlghorithm().getPopulation();
			iterationData[i] = PopulationStatistics.create(population);
		}
		registry.addIterationData(iterationData);
	}

	public PopulationStatisticsRegistry getRegistry() {
		return registry;
	}

	public void setRegistry(PopulationStatisticsRegistry registry) {
		this.registry = registry;
	}

}
